// This code is released under the BSD license.
namespace PugLib.Benchmarks
{
    using System;
    using System.Diagnostics;
    using System.Globalization;

    public sealed class TestResult : IComparable<TestResult>
    {
        private readonly Stopwatch _stopwatch = new Stopwatch();
        private double _duration;

        private string _testName = string.Empty;


        public TestResult(string testName)
        {
            TestName = testName;
        }

        public double Duration
        {
            get { return _duration; }
            set { _duration = value; }
        }

        public string TestName
        {
            get { return _testName; }
            set { _testName = value; }
        }

        public string FormattedDuration
        {
            get { return string.Format(CultureInfo.InvariantCulture, "{0:N2}", _duration); }
        }

        #region IComparable<TestResult> Members

        int IComparable<TestResult>.CompareTo(TestResult other)
        {
            return Duration.CompareTo(other.Duration);
        }

        #endregion

        public override string ToString()
        {
            return string.Concat(TestName, ":", Duration);
        }

        public void Start()
        {
            Duration = 0;
            _stopwatch.Start();
        }

        public void Stop()
        {
            _stopwatch.Stop();
            Duration = _stopwatch.Elapsed.TotalSeconds;
        }

        public void Reset()
        {
            _stopwatch.Reset();
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            TestResult trd = obj as TestResult;
            if (trd == null)
            {
                return false;
            }
            return ((0 == string.CompareOrdinal(trd.TestName, TestName)) && (Duration == trd.Duration));
        }

        public static bool operator !=(TestResult left, TestResult right)
        {
            return !left.Equals(right);
        }

        public static bool operator ==(TestResult left, TestResult right)
        {
            return left.Equals(right);
        }

        public static bool operator >(TestResult left, TestResult right)
        {
            return left.Duration > right.Duration;
        }

        public static bool operator <(TestResult left, TestResult right)
        {
            return left.Duration < right.Duration;
        }

        public bool Compare(TestResult input)
        {
            return this == input;
        }
    }
}